import os
from XEdu.hub import Workflow as wf
import csv
from BaseNN import nn
import numpy as np

model2 = nn('cls')
test_path = 'csv/test_new.csv'
pre_num = 0

def cal_accuracy(y, pred_y):
    res = pred_y.argmax(axis=1)
    tp = np.array(y)==np.array(res)
    acc = np.sum(tp)/ y.shape[0]
    return acc

directory_path = 'checkpoints/20240608'
extension = '.pth'

for i in range(100):
    file_path = os.path.join(directory_path, str(i) + extension)
    test_x = np.loadtxt(test_path, dtype=float, delimiter=',',skiprows=0,usecols=range(0,42)) 
    res = model2.inference(test_x, checkpoint=file_path)
# 获取最后一列的真实值
    test_y = np.loadtxt(test_path, dtype=float, delimiter=',',skiprows=0,usecols=42) 
# 定义一个计算分类正确率的函数
# 计算分类正确率
    num = cal_accuracy(test_y, res)
    if num > pre_num:
        os.rename(file_path, 'checkpoints/20240608/best.pth')
        pre_num = num
        print(num)
